<?php
class MHDEV_View_Helper_GetLastThread extends Zend_View_Helper_Abstract
{
	public function GetLastThread($post_uid)
	{
		// lang
		$translate	= Zend_Registry::get('lang');
		
		// db
		$threads 	= new Threads();
		$posts		= new Posts();
		$user		= new User();
		
		// helper
		$date		= new MHDEV_View_Helper_Datetime();
		$author		= new MHDEV_View_Helper_GetAuthor();
		$url		= new Zend_View_Helper_Url();
		$base		= new MHDEV_View_Helper_BaseUrl();
		$clean		= new MHDEV_View_Helper_CleanUrlTitle();
		
		// config
		$config		= Zend_Registry::get('config');
		
		// query
		$getPost	= $posts->fetchAll('is_deleted = 0 AND uid = ' . $post_uid);
		
		if($getPost->count() > 0)
		{
			$post	= $getPost->toArray();

			if($post[0]['is_public'] == 1)
			{
				$getThread	= $threads->fetchAll('uid = ' . $post[0]['thread_uid']);
				
				if($getThread->count() > 0)
				{
					$thread		= $getThread->toArray();
					
					if($thread[0]['is_public'] == 0)
					{
						return $translate->_('error_last_reply_1');
					}
					elseif($thread[0]['is_deleted'] != 1)
					{
						$last_post_url	= $url->url(array('controller' => 'posts', 'action' => 'getLastPost', 'uid' => $post[0]['thread_uid']), 'last_post', true);
						
						if(strlen($thread[0]['title']) > $config->stringLengthTopics)
						{
							$title		= substr($thread[0]['title'], 0, (int)$config->stringLengthTopics). ' ...';
						}
						else
						{
							$title		= $thread[0]['title'];
						}
						
						$thread_url		= $url->url(array('controller' => 'posts', 'uid' => $post[0]['thread_uid'], 'title' => $clean->CleanUrlTitle($thread[0]['title'])), 'threads', true);
						$thread_title 	= '<a href="' . $thread_url . '">' . htmlentities($title, null, 'utf-8') . '</a>';
						$thread_title	.= '&nbsp;<a href="' . $last_post_url . '"><img src="' . $base->BaseUrl() . '/static/gfx/last_post.gif" alt="' . $translate->_('topic_last_post_img') . '" title="' . $translate->_('topic_last_post_img') . '" /></a>';
						$post_date		= $date->datetime($post[0]['create_date'],'dd. MMMM yy', true);
						
						$user_uid		= $post[0]['user_uid'];
						$user_url		= $url->url(array('controller' => 'profile', 'uid' => $user_uid), 'profile', true);
						$username		= '<a href="' . $user_url . '">' . $author->GetAuthor($user_uid) . '</a>';
						
						$values	= array('%date' => $post_date, '%thread' => $thread_title, '%user' => $username);
						return MHDEV_Sprintf($translate->_('board_last_reply'), $values);
					}
					else
					{
						return $translate->_('error_last_reply_2');
					}
				}
				else
				{
					return $translate->_('error_last_reply');
				}
			}
			else
			{
				return $translate->_('error_last_reply_1');
			}
		}
		else
		{
			return $translate->_('error_last_reply_3');
		}
	}
}